package com.qdu.bussiness.service.impl;

import java.util.List;

import com.qdu.bussiness.constant.BussinessConstant;
import com.qdu.bussiness.domain.BusInventory;
import com.qdu.bussiness.mapper.BusInventoryMapper;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.utils.DateUtils;
import com.qdu.common.utils.SecurityUtils;
import com.qdu.common.utils.id.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdu.bussiness.mapper.BusDeliveryMapper;
import com.qdu.bussiness.domain.BusDelivery;
import com.qdu.bussiness.service.IBusDeliveryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库信息Service业务层处理
 * 
 * @author qdu
 * @date 2022-03-06
 */
@Service
public class BusDeliveryServiceImpl implements IBusDeliveryService 
{
    @Autowired
    private BusDeliveryMapper busDeliveryMapper;

    @Autowired
    private BusInventoryMapper busInventoryMapper;
    /**
     * 查询出库信息
     * 
     * @param deliveryId 出库信息主键
     * @return 出库信息
     */
    @Override
    public BusDelivery selectBusDeliveryByDeliveryId(Long deliveryId)
    {
        return busDeliveryMapper.selectBusDeliveryByDeliveryId(deliveryId);
    }

    /**
     * 查询出库信息列表
     * 
     * @param busDelivery 出库信息
     * @return 出库信息
     */
    @Override
    public List<BusDelivery> selectBusDeliveryList(BusDelivery busDelivery)
    {
        return busDeliveryMapper.selectBusDeliveryList(busDelivery);
    }

    /**
     * 新增出库信息
     * 
     * @param busDelivery 出库信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBusDelivery(BusDelivery busDelivery)
    {
        // 创建出库记录
        busDelivery.setDeliveryId(IdUtil.getId());
        busDelivery.setCreateBy(SecurityUtils.getUsername());
        busDelivery.setCreateTime(DateUtils.getNowDate());
        busDelivery.setDeliveryTime(DateUtils.getNowDate());
        int res = busDeliveryMapper.insertBusDelivery(busDelivery);
        if(res < 1){
            return AjaxResult.error("出库失败");
        }
        // 操作库存
        BusInventory busInventory = busInventoryMapper.selectBusInventoryByInventoryId(busDelivery.getInventoryId());
        Long num = busInventory.getItemNum() - busDelivery.getDeliveryNum();
        BusInventory updateDto = new BusInventory();
        if(num < 0){
            throw new RuntimeException("出库数量大于库存数量！");
        }else if(num == 0){
            // 判断库存数量是否为0
            updateDto.setInventoryStatus(BussinessConstant.INVENTORY_STATUS_OTHERS);
        }
        // 如果是 报废出库，直接将库存状态修改为 报废
        if(BussinessConstant.DELIVERY_STATUS_SCRAPED.equals(busDelivery.getDeliveryType())){
            // 如果报废出库的数量不是所有数量，直接报错
            if(num != 0){
                throw new RuntimeException("报废出库时，请将当前位置库存物料全部出库！");
            }
            updateDto.setInventoryStatus(BussinessConstant.INVENTORY_STATUS_SCRAPED);
        }
        updateDto.setInventoryId(busDelivery.getInventoryId());
        updateDto.setItemNum(num);
        int res2 = busInventoryMapper.updateBusInventory(updateDto);
        if(res2 < 1){
            throw new RuntimeException("库存数量修改失败！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改出库信息
     * 
     * @param busDelivery 出库信息
     * @return 结果
     */
    @Override
    public int updateBusDelivery(BusDelivery busDelivery)
    {
        busDelivery.setUpdateTime(DateUtils.getNowDate());
        return busDeliveryMapper.updateBusDelivery(busDelivery);
    }

    /**
     * 批量删除出库信息
     * 
     * @param deliveryIds 需要删除的出库信息主键
     * @return 结果
     */
    @Override
    public int deleteBusDeliveryByDeliveryIds(Long[] deliveryIds)
    {
        return busDeliveryMapper.deleteBusDeliveryByDeliveryIds(deliveryIds);
    }

    /**
     * 删除出库信息信息
     * 
     * @param deliveryId 出库信息主键
     * @return 结果
     */
    @Override
    public int deleteBusDeliveryByDeliveryId(Long deliveryId)
    {
        return busDeliveryMapper.deleteBusDeliveryByDeliveryId(deliveryId);
    }
}
